package com.ruoyi.safe.controller.web;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.safe.domain.CommonUser;
import com.ruoyi.safe.domain.dto.UserSearchDTO;
import com.ruoyi.safe.domain.vo.CommonUserVO;
import com.ruoyi.safe.service.ICommonUserService;
import com.ruoyi.system.domain.dto.CommonUserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 项目人员管理Controller
 * 
 * @author buxi
 * @date 2023-11-08
 */
@RestController
@RequestMapping("/common/user")
public class CommonUserController extends BaseController
{
    @Autowired
    private ICommonUserService commonUserService;

    /**
     * 查询项目人员管理列表
     */
    @PreAuthorize("@ss.hasPermi('common:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserSearchDTO userSearch)
    {
        startPage();
        List<CommonUserVO> list = commonUserService.selectCommonUserList(userSearch);
        return getDataTable(list);
    }

    /**
     * 导出项目人员管理列表
     */
    @PreAuthorize("@ss.hasPermi('common:user:export')")
    @Log(title = "项目人员管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserSearchDTO userSearch)
    {
        List<CommonUserVO> list = commonUserService.selectCommonUserList(userSearch);
        ExcelUtil<CommonUserVO> util = new ExcelUtil<CommonUserVO>(CommonUserVO.class);
        util.exportExcel(response, list, "项目人员管理数据");
    }

    /**
     * 获取项目人员管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('common:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(commonUserService.selectCommonUserById(id));
    }

    /**
     * 新增项目人员管理
     */
    @PreAuthorize("@ss.hasPermi('common:user:add')")
    @Log(title = "项目人员管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(Long projectId, Long[] ids)
    {
        return toAjax(commonUserService.insertCommonUser(projectId,ids));
    }

    /**
     * 修改项目人员管理
     */
    @PreAuthorize("@ss.hasPermi('common:user:edit')")
    @Log(title = "项目人员管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommonUser commonUser)
    {
        return toAjax(commonUserService.updateCommonUser(commonUser));
    }

    /**
     * 删除项目人员管理
     */
    @PreAuthorize("@ss.hasPermi('common:user:remove')")
    @Log(title = "项目人员管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(commonUserService.deleteCommonUserByIds(ids));
    }

    /**
     * 查询未分配角色项目列表
     */
    @PostMapping(value = "/unallocated")
    public TableDataInfo getCommonUser(@RequestBody CommonUserDTO commonUserDTO) {
        PageUtils.startPage(commonUserDTO.getPageNum(), commonUserDTO.getPageSize());
        return getDataTable(commonUserService.selectCommonUserList(commonUserDTO));
    }

}
